$(function (){
    let rows= document.getElementsByClassName('row');
    for (let m = 0; m < rows.length; m++) {
       if(m%2==0){
           rows[m].style.background = 'rgb(200,220,230)';
       }else
       {
        rows[m].style.background = 'rgb(170,200,200)';
       }

    }
})
function Add(){
    location.href = '/Shop/AddorEndit';
}

function Endit(id){
    location.href = `/Shop/AddorEndit/${id}`;
}

function Save(){
    let Id = $('#Id').val();
    let ProductName = $('#ProductName').val();
    let Supplier = $('#Supplier').val();
    let Price = $('#Price').val();
    let Stock = $('#Stock').val();
    let Remarks = $('#Remarks').val();

    let obj = {
        Id,
        ProductName,
        Supplier,
        Price,
        Stock,
        Remarks
    }
    console.log(obj);
    $.post('/Shop/Save',obj,function(){
        location.href = '/Shop/Index';
    })
}

function Delete(id){
    let msg ="确定删除吗？"
    if(confirm(msg)){
        location.href = `/Shop/Delete/${id}`;
    }else{
        location.href = '/Shop/Index';
    }
    
}

function Cancal(){
    location.href = '/Shop/Index';
}

Select = 1
function SelectChange(){
    let options = document.getElementsByClassName('option');
    let find = document.getElementById('find');
    for (let i = 0; i < options.length; i++) {
        if(options[i].selected){
            if(options[i].value=="1" )
            {
                find.placeholder = "请输入商品名称"; 
                Select=2;
            }else if(options[i].value=="2" ){
                find.placeholder = "请输入商品Id"; 
                Select=2;
            }else{
                find.placeholder = "请输入库存数量"; 
                Select=3;
            }
        }
    }
}

function Find(){
    let Str = $('#find').val();
    if(Select!=1&&isNaN(Number(Str))){
        alert('请输入纯数字');
        location.href = '/Shop/Index';
    }else if(Str.length==0){
        alert('查找框不允许为空');
        location.href = '/Shop/Index';
    }
    else{
        let obj = {
            Str,
            Select
        }
        console.log(obj);
        $.post('/Shop/Select',obj,function(x){
            console.log(x);
            $('.row').html('');
            x.forEach(item => {
                let html =
                `
                <tr class="row">
                <td>${item.id}</td>
                <td>${item.productName}</td>
                <td>${item.supplier}</td>
                <td>${item.price}</td>
                <td>${item.stock}</td>
                <td>${item.remarks}</td>
                <td>
                    <input type="button" value="编辑" onclick="Endit(${item.id})">
                    <input type="button" value="删除" onclick="Delete(${item.id})">
                </td>
            </tr>
            `
            $('.table').append(html);
            });

            document.getElementById('back').style.display = 'inline-block';
        })
    }
}

function Back(){
    location.href = '/Shop/Index';
}


